package service;

import common.Code;
import common.Message;
import common.MessageType;
import common.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ForgetPwd {
    public static Message ms=new Message();
    public static boolean ForgetPwd(String email,String pwd,String code) {
        boolean re = false;
        User tuser = new User(email, pwd,new Code(code));
        Socket temp=null;
        try {
            temp = new Socket("127.0.0.1", 10086);
            //向服务端发送注册申请
            ObjectOutputStream oos = new ObjectOutputStream(temp.getOutputStream());
            ms = new Message();//向服务端发送忘记密码请求
            ms.setState(MessageType.pwd_Forget);
            oos.writeObject(ms);
            //将用户发送到服务端
            oos.writeObject(tuser);
            //从服务器接收消息，是否注册成功
            ObjectInputStream ois = new ObjectInputStream(temp.getInputStream());
            ms = (Message) ois.readObject();
            if (ms.getState() == MessageType.pwd_ResetSuccess) {
                System.out.println("密码重置成功");
                return true;
            }
        } catch (Exception e) {
            System.out.println("重置密码失败");
            return false;
        }finally {
            try {
                temp.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return re;
    }
}
